using Identity.Infrastructure.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class RolePermissionConfiguration  : IEntityTypeConfiguration<RolePermissionEntity>
{
    public void Configure(EntityTypeBuilder<RolePermissionEntity> builder)
    {
        builder.ToTable("RolePermissions");

        // 联合主键 (RoleId + PermissionId)
        builder.HasKey(rp => new { rp.RoleId, rp.PermissionId });

        // Role 1 - n RolePermission
        builder.HasOne(rp => rp.Role)
               .WithMany(r => r.RolePermissions)
               .HasForeignKey(rp => rp.RoleId)
               .IsRequired()
               .OnDelete(DeleteBehavior.Cascade);

        // Permission 1 - n RolePermission
        builder.HasOne(rp => rp.Permission)
               .WithMany()
               .HasForeignKey(rp => rp.PermissionId)
               .IsRequired()
               .OnDelete(DeleteBehavior.Cascade);
    }
}
